package com.longlong.controller.frontDeskApi;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longlong.config.globalExceptionClass.BusinessException;
import com.longlong.entity.RegistrationCompetition;
import com.longlong.entity.RegistrationForm;
import com.longlong.entity.WxGzhUser;
import com.longlong.entity.WxRemainingLikes;
import com.longlong.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/font")
@Slf4j
public class VoteController {
    @Autowired
    private RegistrationCompetitionMapper registrationCompetitionMapper;
    @Autowired
    private WxGzhUserMapper wxGzhuserMapper;
    @Autowired
    private RegistrationFormMapper registrationFormMapper;

    @Autowired
    private RegistrationRacetrackMapper registrationRacetrackMapper;

    @GetMapping("/getRegistrationInformationVote")
    public Object getRegistrationInformationVote(String competitionId){
        QueryWrapper<RegistrationCompetition> rCWrapper = new QueryWrapper<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String localTime = df.format(now);

        rCWrapper
                .eq("rc.id",competitionId)
                .eq("rf.participate_status",2)
                .lt("rc.competition_finish",localTime);
        return registrationCompetitionMapper.selectRegistrationFormListByCompetitionId(rCWrapper);
    }


    @Autowired
    private WxRemainingLikesMapper wxRemainingLikesMapper;

    @GetMapping("/getRemainingLikes")
    public Object getRemainingLikes(String tokenOpenid){
        //获取当天开始时间
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(),LocalTime.MIN);
        //判断用户是否存在
        QueryWrapper<WxGzhUser> wGUWrapper = new QueryWrapper<>();
        wGUWrapper.eq("openid_gzh",tokenOpenid);
        WxGzhUser wxGzhUser = wxGzhuserMapper.selectOne(wGUWrapper);
        //如果openid再wx用户中不存在则判断此用户不存在
        if(wxGzhUser == null){
            throw new BusinessException(404,"用户不存在,请使用微信公众号打开");
        }

        QueryWrapper<WxRemainingLikes> wRLWrapper = new QueryWrapper<>();
        wRLWrapper.eq("openid_gzh",tokenOpenid);
        WxRemainingLikes wxRemainingLikes = wxRemainingLikesMapper.selectOne(wRLWrapper);
        //判断点赞表中是否有此用户
        if(wxRemainingLikes == null){
            //如果不存在则插入一个
            WxRemainingLikes wxRemainingLikesByInsert = new WxRemainingLikes();
            wxRemainingLikesByInsert.setOpenidGzh(tokenOpenid);
            wxRemainingLikesByInsert.setLastDay(today_start);
            /*
            * 设置每个用户每日可点赞数量！！！！！！！！！！！！！！！！！！！！！！！！！！
            * */
            wxRemainingLikesByInsert.setRemainingLikes(2);
            wxRemainingLikesMapper.insert(wxRemainingLikesByInsert);
        }else{
            //如果存在判断用户的点赞数量是否更新
            wxRemainingLikes.getLastDay();
            if(today_start.isAfter(wxRemainingLikes.getLastDay())){
                //需要将用户的登录时间改为今天并重新为用户更新剩余点赞数量
                wxRemainingLikes.setLastDay(today_start);
                wxRemainingLikes.setRemainingLikes(2);
                wxRemainingLikesMapper.updateById(wxRemainingLikes);
            }
        }
        return wxRemainingLikesMapper.selectOne(wRLWrapper);
    }

    @GetMapping("/workLike")
    public Object workLike(String tokenOpenid,String formId){
        //判断用户是否存在
        QueryWrapper<WxGzhUser> wGUWrapper = new QueryWrapper<>();
        wGUWrapper.eq("openid_gzh",tokenOpenid);
        WxGzhUser wxGzhUser = wxGzhuserMapper.selectOne(wGUWrapper);
        //如果openid在wx用户中不存在则判断此用户不存在
        if(wxGzhUser == null){
            throw new BusinessException(404,"点赞失败，请在微信公众号中打开");
        }

        QueryWrapper<WxRemainingLikes> wRLWrapper = new QueryWrapper<>();
        wRLWrapper.eq("openid_gzh",tokenOpenid);
        WxRemainingLikes wxRemainingLikes = wxRemainingLikesMapper.selectOne(wRLWrapper);
        if(wxRemainingLikes == null){
            throw new BusinessException(404,"点赞失败，请在微信公众号中打开");
        }
        //判断用户是否还有足够的点赞数量
        if(wxRemainingLikes.getRemainingLikes() <= 0){
            throw new BusinessException(404,"点赞失败，剩余点赞数量不足");
        }
        //用户点赞数量减1
        wxRemainingLikes.setRemainingLikes(wxRemainingLikes.getRemainingLikes()-1);
        //更新数据
        wxRemainingLikesMapper.updateById(wxRemainingLikes);
        //通过提交的表单id获取表单为表单的点赞数量加上1
        QueryWrapper<RegistrationForm> rFWrapper = new QueryWrapper<>();
        rFWrapper.eq("id",formId);
        RegistrationForm registrationForm = registrationFormMapper.selectOne(rFWrapper);
        registrationForm.setLikeds(registrationForm.getLikeds()+1);
        registrationFormMapper.updateById(registrationForm);
        return true;
    }










}
